package com.dtwave.wechat.service.provider.web.sys;

import com.dtwave.dubbo.DubboResult;
import com.dtwave.wechat.service.api.dto.req.ReqUserDto;
import com.dtwave.wechat.service.provider.database.entity.UserEntity;
import com.dtwave.wechat.service.provider.dto.UserDto;
import com.dtwave.wechat.service.provider.service.UserService;
import com.dtwave.wechat.service.provider.common.utils.Md5;
import com.dtwave.wechat.service.provider.web.BaseController;
import com.dtwave.wolf.utils.BizPreconditions;
import io.swagger.annotations.Api;
import io.swagger.annotations.ApiOperation;
import org.apache.commons.lang3.StringUtils;
import org.apache.shiro.SecurityUtils;
import org.apache.shiro.authc.UsernamePasswordToken;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.apache.shiro.subject.Subject;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.web.bind.annotation.*;

import javax.servlet.http.HttpServletRequest;
import javax.validation.Valid;
import java.io.Serializable;
import java.util.HashMap;
import java.util.Map;

/**
 * @Auther: liuwenxiang
 * @Date: 2018/12/19 15:01
 * @Description:
 */
@Api(description = "登陆模块")
@Controller
@CrossOrigin
@RequestMapping("${url.common.path}/admin")
public class LoginController extends BaseController {


    @Autowired
    private UserService userService;





    @ApiOperation(value="登陆方法",notes="")
    @RequestMapping(value = "/ajax_login", method = RequestMethod.POST)
    @ResponseBody
    public DubboResult list(@Valid @RequestBody ReqUserDto user, HttpServletRequest request) {

        BizPreconditions.checkNotNull(StringUtils.isNotEmpty(user.getPhone()),"登陆名不能为空");
        Subject subject = SecurityUtils.getSubject();
        UsernamePasswordToken token = new UsernamePasswordToken(user.getPhone(), Md5.getPwd(user.getPassWord()));
        UserDto userEntity  = userService.selectByPhone(user.getPhone());
        Map<String,Object> map=new HashMap<String,Object>();
        if(userEntity==null){
            BizPreconditions.checkNotNull("用户不存在","用户不存在");
        }else if(userEntity!=null&&1==userEntity.getStatus()){
            BizPreconditions.checkNotNull("用户被禁用","用户被禁用");
        }else{
            subject.login(token);
            Serializable tokenFlag = subject.getSession().getId();//后台凭证
            map.put("tokenFlag", (String)tokenFlag);//返回token值给前端作为登陆凭证
            map.put("code","SUCCESS");
        }
        return DubboResult.buildSuccessResult(map);
    }



    //有权限
    @RequestMapping("/admin1")
    @ResponseBody
    @RequiresPermissions("admin:test1")
    public DubboResult hello1() {

        return DubboResult.buildSuccessResult();
    }


    //没设置权限标签
    @RequestMapping("/admin2")
    @ResponseBody
    public DubboResult hello2() {

        return DubboResult.buildSuccessResult();
    }

    //无权限
    @RequestMapping("/admin3")
    @ResponseBody
    @RequiresPermissions("admin:test2")
    public DubboResult hello3() {
        return DubboResult.buildSuccessResult();
    }

}
